#include "playerChoiceDialog.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>

PlayerChoiceDialog::PlayerChoiceDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("选择阵营");
    setFixedSize(300, 200);

    QVBoxLayout *layout = new QVBoxLayout(this);

    QLabel *label = new QLabel("请选择您的阵营：", this);
    layout->addWidget(label);

    buttonGroup = new QButtonGroup(this);
    QRadioButton *redButton = new QRadioButton("红方（先手）", this);
    QRadioButton *blackButton = new QRadioButton("黑方（后手）", this);
    // 默认选择红色方
    redButton->setChecked(true);

    buttonGroup->addButton(redButton, 1);  // 1代表红方
    buttonGroup->addButton(blackButton, 2); // 2代表黑方

    layout->addWidget(redButton);
    layout->addWidget(blackButton);

    QPushButton *confirmButton = new QPushButton("确认", this);
    connect(confirmButton, &QPushButton::clicked, this, [this](){
        if (buttonGroup->checkedId() != -1) {
            accept(); // 关闭对话框并返回QDialog::Accepted
        }
    });

    layout->addSpacerItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
    layout->addWidget(confirmButton);

    connect(buttonGroup, &QButtonGroup::idClicked,
            this, &PlayerChoiceDialog::onSelectionMade);

    setStyleSheet(
        "QDialog {"
        "  background: #f5f5f5;"
        "  border: 1px solid #ccc;"
        "}"
        "QLabel {"
        "  font-size: 16px;"
        "  color: #333;"
        "}"
        "QRadioButton {"
        "  font-size: 14px;"
        "  padding: 8px;"
        "}"
        "QPushButton {"
        "  min-width: 100px;"
        "  padding: 8px;"
        "  background: #4CAF50;"
        "  color: white;"
        "  border: none;"
        "}"
        );
}

void PlayerChoiceDialog::onSelectionMade(int side) {
    m_selectedSide = side;
}
